package com.xxd.rpt2.Entity

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import org.hibernate.annotations.Fetch

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.ManyToOne
import javax.persistence.Table

@Entity
@Table(name = "rpt2_user")
@JsonIgnoreProperties(value = [ "hibernateLazyInitializer", "handler" ])
class User {

    @Id
    @GeneratedValue
    int id
    String name
    String code
    String password
    String salt
    String mobile
    String email
    String addr
    @ManyToOne
    @JoinColumn(name="dept_id" ,referencedColumnName = "id")
    Department department

    @ManyToOne
    @JoinColumn(name="area_id" ,referencedColumnName = "id")
    Area area

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name="rpt2_user_role",
            joinColumns = [@JoinColumn(name="user_id",referencedColumnName = "id")],
            inverseJoinColumns = [@JoinColumn(name="role_id",referencedColumnName = "id")])
    List<Role> roleList

    public String getCredentialsSalt() {
        return code + salt
    }
}
